<?php
/* @property ModelCatalogManufacturer model_catalog_manufacturer */
/* @property ModelCatalogCategory model_catalog_category */
class ControllerModuleJournal2SideBlocks extends Controller {

    public function __construct($registry) {
        parent::__construct($registry);
        if (!defined('JOURNAL_INSTALLED')) {
            return;
        }
        $this->load->model('journal2/module');
        $this->load->model('journal2/menu');
    }

    public function index($setting) {
        if (!defined('JOURNAL_INSTALLED')) {
            return;
        }
        if ($this->journal2->mobile_detect->isMobile() && !$this->journal2->mobile_detect->isTablet()) return;

        static $module = 0;

        $module_data = $this->model_journal2_module->getModule($setting['module_id']);
        if (!$module_data || !isset($module_data['module_data']) || !$module_data['module_data']) return;

        $css = array();

        if (Journal2Utils::getProperty($module_data, 'module_data.position')) {
            $css[] = 'position: ' . Journal2Utils::getProperty($module_data, 'module_data.position');
        }
        if (Journal2Utils::getProperty($module_data, 'module_data.offset_top')) {
            $css[] = 'top: ' . Journal2Utils::getProperty($module_data, 'module_data.offset_top', 0) . 'px';
        }

        $this->data['icon'] = Journal2Utils::getIconOptions2(Journal2Utils::getProperty($module_data, 'module_data.icon'));

        if (Journal2Utils::getProperty($module_data, 'module_data.icon_bg_color')) {
            $this->data['icon_bgcolor'] = Journal2Utils::getColor(Journal2Utils::getProperty($module_data, 'module_data.icon_bg_color'));
        }
        $this->data['icon_border'] = implode('; ', Journal2Utils::getBorderCssProperties(Journal2Utils::getProperty($module_data, 'module_data.icon_border')));

        switch (Journal2Utils::getProperty($module_data, 'module_data.module_type', 'block')) {
            case 'button':
                $this->data['type'] = 'button';
                $this->data['url'] = $this->model_journal2_menu->getLink(Journal2Utils::getProperty($module_data, 'module_data.link'));
                $this->data['target'] = Journal2Utils::getProperty($module_data, 'module_data.new_window') ? 'target="_blank"' : '';
                if (Journal2Utils::getProperty($module_data, 'module_data.icon_bg_hover_color')) {
                    $this->data['icon_bg_hover_color'] = Journal2Utils::getColor(Journal2Utils::getProperty($module_data, 'module_data.icon_bg_hover_color'));
                } else {
                    $this->data['icon_bg_hover_color'] = false;
                }
                $position = Journal2Utils::getProperty($module_data, 'module_data.alignment', 'left');
                $offset = Journal2Utils::getProperty($module_data, 'module_data.offset_side', 0) . 'px';
                $css[] = "{$position}: {$offset}";
                break;
            case 'block':
                $this->data['type'] = 'block';
                $this->data['url'] = 'index.php?route=module/journal2_side_blocks/load&amp;module_id=' . $setting['module_id'];
                $this->data['content_padding'] = Journal2Utils::getProperty($module_data, 'module_data.content_padding', 0) . 'px';
                if (Journal2Utils::getProperty($module_data, 'module_data.content_width')) {
                    $css[] = 'width: ' . Journal2Utils::getProperty($module_data, 'module_data.content_width', 50) . 'px';
                    $css[] = (Journal2Utils::getProperty($module_data, 'module_data.alignment') === 'left' ? 'left' : 'right') . ': -' . Journal2Utils::getProperty($module_data, 'module_data.content_width', 50) . 'px';
                }
                if (Journal2Utils::getProperty($module_data, 'module_data.content_bg_color')) {
                    $this->data['content_bgcolor'] = Journal2Utils::getColor(Journal2Utils::getProperty($module_data, 'module_data.content_bg_color'));
                }
                break;
        }

        $this->data['alignment'] = Journal2Utils::getProperty($module_data, 'module_data.alignment');
        $this->data['css'] = implode('; ', $css);

        $this->data['module'] = $module++;

        $this->template = 'journal2/template/journal2/module/side_blocks.tpl';
        $this->render();
    }

    public function load() {
        $module_id = isset($this->request->get['module_id']) ? $this->request->get['module_id'] : -1;

        $module_data = $this->model_journal2_module->getModule($module_id);
        if (!$module_data || !isset($module_data['module_data']) || !$module_data['module_data']) return;

        $this->response->setOutput(Journal2Utils::getProperty($module_data, 'module_data.content.' . $this->config->get('config_language_id')));
    }

}
